import {
  CreateExternalSite,
  ExternalSite,
  ExternalSiteGroup,
  ExternalSitesTag,
  GetExternalSitesImportProgressResponse,
  GetExternalSitesTagListResponse,
  ImportExternalSitesResponse,
} from '@/types/nav/api';
import apiClient from '../apiClient';
import { PaginationProps, PaginationResponse } from '@/common/types';
import { AxiosProgressEvent } from 'axios';

type SitesTag = {
  tags?: string;
};

type CreateExternalSitesTag = {
  name: string;
  sort_order?: number;
};

export enum ExternalSitesApi {
  GetExternalSitesList = '/openapi/v2/external-sites/list',
  CreateExternalSite = '/openapi/v2/external-sites/create',
  UpdateExternalSite = '/openapi/v2/external-sites/update/:id',
  DeleteExternalSite = '/openapi/v2/external-sites/delete',
  GetExternalSitesTagList = '/openapi/v2/external-sites/tags/list',
  CreateExternalSitesTag = '/openapi/v2/external-sites/tags/create',
  UpdateExternalSitesTag = '/openapi/v2/external-sites/tags/update/:id',
  UpdateExternalSitesTags = '/openapi/v2/external-sites/tags/update',
  DeleteExternalSitesTag = '/openapi/v2/external-sites/tags/delete/:id',
  ImportExternalSites = '/openapi/v2/external-sites/import/excel',
  ImportExternalSitesTerminate = '/openapi/v2/external-sites/import/terminate',
  GetExternalSitesImportProgress = '/openapi/v2/external-sites/import/progress/:id',
}

/**
 * 获取外部站点列表
 * @returns
 */
const getExternalSitesList = (params: PaginationProps<SitesTag>) =>
  apiClient.get<PaginationResponse<ExternalSiteGroup>>({
    url: ExternalSitesApi.GetExternalSitesList,
    params,
  });

/**
 * 创建外部站点
 * @param data
 * @returns
 */
const createExternalSite = (data: CreateExternalSite) =>
  apiClient.post({
    url: ExternalSitesApi.CreateExternalSite,
    data,
  });

/**
 * 更新外部站点
 * @param id
 * @param data
 * @returns
 */
const updateExternalSite = (id: string, data: ExternalSite) =>
  apiClient.post({
    url: ExternalSitesApi.UpdateExternalSite.replace(':id', id),
    data,
  });

/**
 * 删除外部站点
 * @param site_ids
 * @returns
 */
const deleteExternalSite = (site_ids: string[]) =>
  apiClient.post({
    url: ExternalSitesApi.DeleteExternalSite,
    data: { site_ids },
  });

/**
 * 获取外部站点标签
 * @returns
 */
const getExternalSitesTagList = () =>
  apiClient.get<GetExternalSitesTagListResponse>({
    url: ExternalSitesApi.GetExternalSitesTagList,
  });

/**
 * 创建外部站点标签
 * @param data
 * @returns
 */
const createExternalSitesTag = (data: CreateExternalSitesTag) =>
  apiClient.post<ExternalSitesTag>({
    url: ExternalSitesApi.CreateExternalSitesTag,
    data,
  });

/**
 * 更新外部站点标签
 * @param id
 * @param data
 * @returns
 */
const updateExternalSitesTag = (id: string, data: Pick<ExternalSitesTag, 'name' | 'sort_order'>) =>
  apiClient.post<ExternalSitesTag>({
    url: ExternalSitesApi.UpdateExternalSitesTag.replace(':id', id),
    data,
  });

/**
 * 批量更新外部站点标签
 * @param tags
 * @returns
 */
const updateExternalSitesTags = (
  tags: (Pick<ExternalSitesTag, 'name' | 'sort_order'> & { tag_id: string })[],
) =>
  apiClient.post<ExternalSitesTag>({
    url: ExternalSitesApi.UpdateExternalSitesTags,
    data: {
      tags,
    },
  });

/**
 * 删除外部站点标签
 * @param id
 * @returns
 */
const deleteExternalSitesTag = (id: string) =>
  apiClient.post({
    url: ExternalSitesApi.DeleteExternalSitesTag.replace(':id', id),
  });

/**
 * 导入外部站点
 * @param data
 * @param onUploadProgress
 * @param signal
 * @returns
 */
const importExternalSites = (
  data: FormData,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
  signal?: AbortSignal,
) =>
  apiClient.post<ImportExternalSitesResponse>({
    url: ExternalSitesApi.ImportExternalSites,
    withCredentials: true,
    timeout: 60000,
    data,
    onUploadProgress,
    headers: { 'Content-Type': 'multipart/form-data' },
    signal,
  });

/**
 * 获取导入外部站点进度
 * @param id
 * @returns
 */
const getExternalSitesImportProgress = (id: string, signal?: AbortSignal) =>
  apiClient.get<GetExternalSitesImportProgressResponse>({
    url: ExternalSitesApi.GetExternalSitesImportProgress.replace(':id', id),
    signal,
  });

/**
 * 终止导入外部站点
 * @param task_id
 * @returns
 */
const terminateExternalSitesImport = (task_id: string) =>
  apiClient.post({
    url: ExternalSitesApi.ImportExternalSitesTerminate,
    data: {
      task_id,
    },
  });
export default {
  getExternalSitesList,
  getExternalSitesTagList,
  createExternalSite,
  updateExternalSite,
  deleteExternalSite,
  createExternalSitesTag,
  updateExternalSitesTag,
  updateExternalSitesTags,
  deleteExternalSitesTag,
  importExternalSites,
  terminateExternalSitesImport,
  getExternalSitesImportProgress,
};
